package org.lex.keytool.ui.common;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.lex.keytool.Swings;
import org.lex.keytool.ui.builder.FileChooserBuilder;

public class FilePane extends JPanel {
	public enum Type {
		OPEN_FILE, SAVE_FILE
	};

	public static void main(String[] args) {
		Swings.show(new FilePane(Type.OPEN_FILE));
	}

	private JButton button;
	private JLabel label;
	private JTextField textField;

	public FilePane(Type type) {
		initComponents();

		boolean isOpen = true;
		switch (type) {
		case OPEN_FILE:
			isOpen = true;
			break;
		case SAVE_FILE:
			isOpen = false;
			break;
		default:
			isOpen = true;
			System.err.println("Type=" + type + " not understand");
		}
		FileChooserBuilder.addHandler(button, textField, isOpen);
	}

	public void setTitle(String text) {
		this.label.setText(text);
	}

	public void setButton(String text) {
		this.button.setText(text);
	}

	public String getFilePath() {
		return textField.getText();
	}

	private void initComponents() {
		setLayout(new GridBagLayout());
		GridBagConstraints gc;

		label = new JLabel();
		label.setText("File");
		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.insets = new Insets(0, 0, 0, 5);
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		add(label, gc);

		textField = new JTextField();
		gc = new GridBagConstraints();
		gc.gridx = 1;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.weightx = 10.0;
		gc.insets = new Insets(0, 0, 0, 5);
		add(textField, gc);

		button = new JButton();
		button.setText("Open...");
		gc = new GridBagConstraints();
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.gridx = 2;
		add(button, gc);
	}
}
